// 匹配a
function match1(string) {
    for(let c of string) {
        if(c =="a")
            return true
    }
    return false
}


console.log('a', match1("I am groot"), 'match1')

// 匹配ab
function match2(string) {
    let foundA = false;
    for(let c of string) {
        if(c =="a")
          foundA = true
        else if(foundA && c == "b") {
            return true
        } else {
            foundA = false
        }
    }
    return false
}

console.log('ab',match2("I am groot"), 'match2')


// 匹配abcdef
function match3(string) {
    let foundA = false;
    let foundB = false;
    let foundC = false;
    let foundD = false;
    let foundE = false;
    for(let c of string) {
        if(c =="a")
          foundA = true
        else if(foundA && c == "b") {
            foundB = true
        }else if(foundB && c == "c") {
            foundC = true
        }else if(foundC && c == "d") {
            foundD = true
        }else if(foundD && c == "e") {
            foundE = true
        }else if(foundE && c == "f") {
            return true
        } else {
            let foundA = false;
            let foundB = false;
            let foundC = false;
            let foundD = false;
            let foundE = false;
        }
    }
    return false
}
console.log('abcdef',match3("I am groot"), 'match3')

// 匹配abcdef
function match4(string) {
    let state = start;
    for(let c of string) {
        state = state(c)
    }
    return state === end;
}

function start(c) {
    if (c === "a") {
        return foundA
    } else {
        return start;
    }
}

function end(c) {
    return end;
}

function foundA(c) {
    if (c === 'b') {
        return foundB;
    } else {
        return start(c)
    }
}
function foundB(c) {
    if (c === 'c') {
        return foundC;
    } else {
        return start(c)
    }
}
function foundC(c) {
    if (c === 'd') {
        return foundD;
    } else {
        return start(c)
    }
}
function foundD(c) {
    if (c === 'e') {
        return foundE;
    } else {
        return start(c)
    }
}
function foundE(c) {
    if (c === 'f') {
        return end;
    } else {
        return start(c)
    }
}

console.log('abcdef',match4("abcdef"), "match4")


